package com.pilot.game.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.pilot.game.service.*;
import com.pilot.game.web.rest.vm.CommonResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * REST controller for managing Kind.
 */
@RestController
@RequestMapping("/api")
public class UtilsResource {

    private final Logger log = LoggerFactory.getLogger(UtilsResource.class);

    private final FileUploadService fileUploadService;
    private final TulinRobotService tulinRobotService;
    private final QrCodeService qrCodeService;
    private final SmsServices smsServices;
    private final MailService mailService;
    private final Jobs jobs;

    public UtilsResource(FileUploadService fileUploadService, TulinRobotService tulinRobotService,
                         QrCodeService qrCodeService, SmsServices smsServices, Jobs jobs, MailService mailService) {
        this.fileUploadService = fileUploadService;
        this.tulinRobotService = tulinRobotService;
        this.qrCodeService = qrCodeService;
        this.smsServices = smsServices;
        this.mailService = mailService;
        this.jobs = jobs;
    }

    /**
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/utils/uploadFile")
    @Timed
    public ResponseEntity uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("fileName") String fileName) throws IOException {
        log.debug("REST post request to fileName : {}", file.getOriginalFilename());
        String fileUrl = fileUploadService.uploadFile(file.getBytes(), fileName);
        return ResponseEntity.ok(fileUrl);
    }

    /**
     * @param msg
     * @return
     * @throws IOException
     */
    @GetMapping("/utils/robot")
    @Timed
    public ResponseEntity robot(@RequestParam("msg") String msg) throws IOException {
        log.debug("REST request to talk : {}", msg);
        String talk = tulinRobotService.talk(msg);
        return ResponseEntity.ok(talk);
    }

    /**
     * @param content
     * @return
     * @throws IOException
     */
    @GetMapping("/utils/qrCode")
    @Timed
    public ResponseEntity qrCode(@RequestParam("content") String content) {
        log.debug("REST request to qrCode content : {}", content);
        String qrCode = qrCodeService.generateQrCode(content);
        return ResponseEntity.ok(qrCode);
    }

    /**
     * 发送邮件验证码
     *
     * @return result
     */
    @GetMapping("/utils/mailRegisterCode")
    @Timed
    public CommonResponse sendMail(@RequestParam("email") String email) {
        log.debug("REST request to send mail");

        CommonResponse commonResponse = new CommonResponse();

        try {
            mailService.sendRegisterEmail(email);
            commonResponse.setStatus(true);

        } catch (Exception e) {
            commonResponse.setStatus(false);
        }
        return commonResponse;
    }

    /**
     * 发送短信验证码通过短信模版
     *
     * @param mobile mobile
     * @return result
     */
    @GetMapping("/utils/smsMsg")
    @Timed
    public CommonResponse smsMsg(@RequestParam("mobile") String mobile) {
        CommonResponse commonResponse = new CommonResponse();
        log.debug("REST request to smsMsg content : {}", mobile);
        if (smsServices.sendSmsMsg(mobile)) {
            commonResponse.setStatus(true);
            return commonResponse;
        }
        commonResponse.setStatus(false);
        return commonResponse;
    }

    /**
     * 发送短信,短信内容自定义
     *
     * @param mobile  mobile
     * @param content content
     * @return result
     */
    @GetMapping("/utils/smsMsgContent")
    @Timed
    public ResponseEntity smsMsg(@RequestParam("mobile") String mobile, String content) {
        log.debug("REST request to smsMsg mobile:{} content : {}", mobile, content);
        if (smsServices.sendSmsMsg(mobile, content)) {
            return ResponseEntity.ok().build();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 发送语音验证码
     *
     * @param mobile mobile
     * @return result
     */
    @GetMapping("/utils/smsVoiceMsg")
    @Timed
    public ResponseEntity smsVoiceMsg(@RequestParam("mobile") String mobile) {
        log.debug("REST request to smsMsg mobile:{}", mobile);
        if (smsServices.sendSmsVoice(mobile)) {
            return ResponseEntity.ok().build();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 获取短信记录
     *
     * @return result
     */
    @GetMapping("/utils/smsMsgHistory")
    @Timed
    public ResponseEntity getSmsMsgHistory() {
        log.debug("REST request to smsMsg getSmsMsgHistory");
        return ResponseEntity.ok(smsServices.getSmsMsgHistory());
    }

    /**
     * 查询余额
     *
     * @return result
     */
    @GetMapping("/utils/smsAccountBalance")
    @Timed
    public ResponseEntity getSmsAccountBalance() {
        log.debug("REST request to smsMsg getSmsAccountBalance");
        return ResponseEntity.ok(smsServices.getAccountBalance());
    }


    /**
     * 心跳
     *
     * @return result
     */
    @GetMapping("/utils/heartBeat")
    @Timed
    public CommonResponse heartBeat(HttpServletRequest request) {
        log.debug("REST request to heartBeat");
        log.info("IP:{}", request.getRemoteHost());
        log.info("BrowserDetails:{}", request.getHeader("User-Agent"));
        CommonResponse commonResponse = new CommonResponse();
        commonResponse.setStatus(true);
        commonResponse.setMsg(jobs.hearBeat());
        return commonResponse;
    }
}
